﻿using System;
using System.Linq;
using System.Threading;
using Microsoft.Phone.Data.Linq;

namespace Samples.Radio.Agent.Data
{
    public class MutexWrapper : IDisposable
    {
        public AudioStreamingDataContext Database { get; set; }

        public MutexWrapper()
        {
            AudioStreamingDataContext.DatabaseMutex.WaitOne();
            Database = AudioStreamingDataContext.ValidateAndOpenDatabase();
        }

        public void ReopenDatabase()
        {
            if (Database != null)
            {
                Database.Dispose();
            }
            Database = AudioStreamingDataContext.ValidateAndOpenDatabase();

        }

        public void Dispose()
        {
            Database.Dispose();
            AudioStreamingDataContext.DatabaseMutex.ReleaseMutex();
        }
    }

    public partial class AudioStreamingDataContext
    {
        public static Mutex DatabaseMutex = new Mutex(false, "AudioStreamingMutex");

        private const string ConnectionString = "Data Source=isostore:/AudioStreamingDB.sdf";
        private const int CurrentSchemaVersion = 1;

        private static bool verified;

        public static AudioStreamingDataContext ValidateAndOpenDatabase()
        {
            var database = new AudioStreamingDataContext(ConnectionString);

            if (verified) return database;
            // Debugging only - comment these lines to prevent deletion of db each run
            //if(Database.DatabaseExists())
            //{
            //    Database.DeleteDatabase();
            //}

            if (!database.DatabaseExists())
            {
                database.CreateDatabase();
            }


            var updater = database.CreateDatabaseSchemaUpdater();
            if (updater.DatabaseSchemaVersion != CurrentSchemaVersion)
            {
                // For version increments simply uncomment and extend the following
                //if(updater.DatabaseSchemaVersion<2)
                //{
                //    // eg Add "Description" column to the Track table
                //    updater.AddColumn<Track>("Description");
                //    updater.Execute();
                //}

                updater.DatabaseSchemaVersion = CurrentSchemaVersion;
                updater.Execute();
            }

            verified = true;

            // At this point the database is up and ready to use!
            return database;
        }

        public StreamingConfiguration CurrentConfiguration
        {
            get
            {
                var conf = StreamingConfigurations.FirstOrDefault();
                if (conf == null)
                {
                    conf = new StreamingConfiguration {Id = Guid.NewGuid(), 
                        PlaybackModeIsStreaming = true,
                        CurrentStreamName = string.Empty};
                    StreamingConfigurations.InsertOnSubmit(conf);
                    SubmitChanges();
                }
                return conf;
            }
        }
    }
}
